package com.zx.mes.hyl.auth.rest;
import com.zx.mes.hyl.auth.model.Tuser;
import com.zx.mes.hyl.auth.pagemodel.Resource;
import com.zx.mes.hyl.auth.pagemodel.Role;
import com.zx.mes.hyl.auth.service.RoleServiceI;
import com.zx.mes.hyl.common.msg.ObjectRestResponse;
import com.zx.mes.hyl.common.msg.TableResultResponse;
import com.zx.mes.hyl.common.pagemodel.Json;
import com.zx.mes.hyl.common.pagemodel.JsonResult;
import com.zx.mes.hyl.common.pagemodel.LayPageHelper;
import com.zx.mes.hyl.common.pagemodel.Tree;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
import java.util.UUID;


/**
 * 角色控制器
 * 
 * @author hyl
 * @date 2017-12-22
 */
@Controller
@RequestMapping("/roleController")
public class RoleController{

	@Autowired
	private RoleServiceI roleService;

	private static final Logger logger=Logger.getLogger(RoleController.class);

	@ResponseBody
	@RequestMapping("/list")
	public JsonResult<List> list(Role role, LayPageHelper ph ){
		JsonResult<List> jsonResult=new JsonResult<>();
		List<Role> roleList=roleService.findAll(role,ph);

		jsonResult.setIS_SUCCESS(true);
		jsonResult.setCode(0);
		jsonResult.setData(roleList);
		jsonResult.setCount((int) roleService.count(role));
		jsonResult.setMsg("");
		jsonResult.setStatus(200);
		return jsonResult;
	}



	@RequestMapping("/add")
	@ResponseBody
	public Json add(Role role) {

		Json j = new Json();
		//由后台产生唯一 id
		role.setId(UUID.randomUUID().toString());
		try {
			roleService.add(role);
			j.setSuccess(true);
			j.setMsg("添加成功！");
			j.setStatus(200);
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg("添加失败！");
		}

		return j;
	}


	@RequestMapping("/edit")
	@ResponseBody
	public Json edit(Role role) {
		Json j = new Json();
		try {
			roleService.edit(role);
			j.setSuccess(true);
			j.setMsg("编辑成功！");
			j.setStatus(200);
		} catch (Exception e) {
			j.setMsg("编辑失败！");
		}

		return j;
	}



	@RequestMapping("/allTree")
	@ResponseBody
	public List<Tree> allTree(String userId) {
		Tuser tuser=new Tuser();
		tuser.setId(userId);
		return roleService.allTree(tuser);
	}

	@RequestMapping("/getAll")
	@ResponseBody
	public List<Tree> getAll() {
		return roleService.getAll();
	}

	@RequestMapping("/getOne")
	@ResponseBody
	public ObjectRestResponse<List> getOne(String id){
		ObjectRestResponse<List> response=new ObjectRestResponse<>();
		response.setData(roleService.get(id).getResourceList());
		return response;
	}

	@RequestMapping("/delete")
	@ResponseBody
	public Json delete(String id) {
		Json j = new Json();
		roleService.delete(id);
		j.setMsg("删除成功！");
		j.setSuccess(true);
		j.setStatus(200);
		return j;
	}


	@RequestMapping("/grant")
	@ResponseBody
	public Json grant(Role role) {
		Json j = new Json();
		roleService.grant(role);
		j.setMsg("授权成功！");
		j.setSuccess(true);
		j.setStatus(200);
		return j;
	}

}
